package com.jeeplus.invoice.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class WechatUtil {
	
	/**
	 * 获取微信OpenID
	 * @param code
	 * @return
	 */
	public static String getOpenID(String code){
		//微信公众平台认证接口地址
		String url = "https://api.weixin.qq.com/sns/jscode2session";
		//认证参数构建
		Map<String,String> param = new HashMap<String,String>();
		PropertiesLoader p = new PropertiesLoader("/appid.properties");
		String app_Id = p.getProperty("appid");
		String app_secret = p.getProperty("appsecret");
		param.put("appid", app_Id);
		param.put("secret", app_secret);
		param.put("js_code", code);
		param.put("grant_type", "authorization_code");
		//返回认证结果
		String wxResult = HttpClientUtil.doGet(url,param);
		//格式化处理
		Map<String,Object> model = JsonUtil.jsonToMap(wxResult);
		//获取OpenID对象
		Object objOpenid = model.get("openid");
		//从OpenID对象得到OpenID字符串，获取失败返回失败结果
		String openid = objOpenid == null ? "" : objOpenid.toString();
		return openid ;
	}
	
	/**
	 * 获取微信OpenID
	 * @param appId
	 * @param secret
	 * @param code
	 * @return
	 */
	public static String getOpenID(String appId,String secret,String code){
		//微信公众平台认证接口地址
		String url = "https://api.weixin.qq.com/sns/jscode2session";
		//认证参数构建
		Map<String,String> param = new HashMap<String,String>();
		PropertiesLoader p = new PropertiesLoader("/appid.properties");
		String app_Id = p.getProperty(appId);
		String app_secret = p.getProperty(secret);
		param.put("appid", app_Id);
		param.put("secret", app_secret);
		param.put("js_code", code);
		param.put("grant_type", "authorization_code");
		//返回认证结果
		String wxResult = HttpClientUtil.doGet(url,param);
		//格式化处理
		Map<String,Object> model = JsonUtil.jsonToMap(wxResult);
		//获取OpenID对象
		Object objOpenid = model.get("openid");
		//从OpenID对象得到OpenID字符串，获取失败返回失败结果
		String openid = objOpenid == null ? "" : objOpenid.toString();
		return openid ;
	}
	
	/**
	 * 获取微信SessionKey
	 * @param appId
	 * @param secret
	 * @param code
	 * @return
	 */
	public static String getSessionKey(String appId,String secret,String code){
		//微信公众平台认证接口地址
		String url = "https://api.weixin.qq.com/sns/jscode2session";
		//认证参数构建
		Map<String,String> param = new HashMap<String,String>();
		PropertiesLoader p = new PropertiesLoader("/appid.properties");
		String app_Id = p.getProperty(appId);
		String app_secret = p.getProperty(secret);
		param.put("appid", app_Id);
		param.put("secret", app_secret);
		param.put("js_code", code);
		param.put("grant_type", "authorization_code");
		//返回认证结果
		String wxResult = HttpClientUtil.doGet(url,param);
		//格式化处理
		Map<String,Object> model = JsonUtil.jsonToMap(wxResult);
		//获取OpenID对象
		Object sessionKey = model.get("session_key");
		//从OpenID对象得到OpenID字符串，获取失败返回失败结果
		String session_key = sessionKey == null ? "" : sessionKey.toString();
		return session_key ;
	}
	
	/**
	 * 获取微信SessionKey
	 * @param code
	 * @return
	 */
	public static String getSessionKey(String code){
		//微信公众平台认证接口地址
		String url = "https://api.weixin.qq.com/sns/jscode2session";
		//认证参数构建
		Map<String,String> param = new HashMap<String,String>();
		PropertiesLoader p = new PropertiesLoader("/appid.properties");
		String app_Id = p.getProperty("bh_consumer_appid");
		String app_secret = p.getProperty("bh_consumer_appsecret");
		param.put("appid", app_Id);
		param.put("secret", app_secret);
		param.put("js_code", code);
		param.put("grant_type", "authorization_code");
		//返回认证结果
		String wxResult = HttpClientUtil.doGet(url,param);
		//格式化处理
		Map<String,Object> model = JsonUtil.jsonToMap(wxResult);
		//获取OpenID对象
		Object sessionKey = model.get("session_key");
		//从OpenID对象得到OpenID字符串，获取失败返回失败结果
		String session_key = sessionKey == null ? "" : sessionKey.toString();
		return session_key ;
	}
	
	/**
	 * 获取微信SessionKey和OpenID
	 * @param code
	 * @return
	 */
	public static JSONObject getSessionKeyAndOpenID(String code){
		//微信公众平台认证接口地址
		String url = "https://api.weixin.qq.com/sns/jscode2session";
		//认证参数构建
		Map<String,String> param = new HashMap<String,String>();
		PropertiesLoader p = new PropertiesLoader("/appid.properties");
		String app_Id = p.getProperty("bh_consumer_appid");
		String app_secret = p.getProperty("bh_consumer_appsecret");
		param.put("appid", app_Id);
		param.put("secret", app_secret);
		param.put("js_code", code);
		param.put("grant_type", "authorization_code");
		//返回认证结果
		JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet(url,param));
		return jsonObject ;
	}
	
	/**
	 * 获取微信SessionKey和OpenID
	 * @param appId
	 * @param secret
	 * @param code
	 * @return
	 */
	public static JSONObject getSessionKeyAndOpenID(String appId,String secret,String code){
		//微信公众平台认证接口地址
		String url = "https://api.weixin.qq.com/sns/jscode2session";
		//认证参数构建
		Map<String,String> param = new HashMap<String,String>();
		PropertiesLoader p = new PropertiesLoader("/appid.properties");
		String app_Id = p.getProperty(appId);
		String app_secret = p.getProperty(secret);
		param.put("appid", app_Id);
		param.put("secret", app_secret);
		param.put("js_code", code);
		param.put("grant_type", "authorization_code");
		//返回认证结果
		JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet(url,param));
		return jsonObject ;
	}

}
